package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.GroupActive;
import com.bwie.pojo.GroupJoin;
import com.bwie.service.GroupActiveService;
import com.bwie.mapper.GroupActiveMapper;
import com.bwie.utils.R;
import com.bwie.vo.ActiveVo;
import com.bwie.vo.JoinVo;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
* @author 陶仕豪
* @description 针对表【tb_group_active(拼团活动表)】的数据库操作Service实现
* @createDate 2025-07-27 19:06:34
*/
@Service
public class GroupActiveServiceImpl extends ServiceImpl<GroupActiveMapper, GroupActive>
    implements GroupActiveService{

    @Autowired
    GroupActiveMapper activeMapper;
    @Autowired
    HttpServletRequest request;
    @Override
    public R getActiveList(ActiveVo activeVo) {
        Page<GroupActive> activePage = new Page<>(activeVo.getPageNum(), activeVo.getPageSize());
        QueryWrapper<GroupActive> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(activeVo.getActiveName())){
            wrapper.lambda().like(GroupActive::getActiveName,activeVo.getActiveName());
        }
        Page<GroupActive> page = activeMapper.selectPage(activePage, wrapper);
        return R.OK(page);
    }

    @Override
    public R addActive(GroupActive groupActive) {
        String token = request.getHeader("token");
        if (token == null) {
            return R.ERROR(500, "请先登录");
        }
        String userName = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userName", String.class);
        GroupActive groupActive1 = activeMapper.selectOne(new QueryWrapper<GroupActive>().lambda().eq(GroupActive::getActiveName, groupActive.getActiveName()));
        if(groupActive1!=null){
            return R.ERROR(500,"活动名称已存在");
        }
        long beginTime = groupActive.getBeginTime().getTime();
        long ednTime = groupActive.getEndTime().getTime();
        if(ednTime<beginTime){
            return R.ERROR(500,"截止时间不能小于开始时间");
        }
        groupActive.setCreateBy(userName);
        groupActive.setCreateTime(new Date());
        activeMapper.insert(groupActive);
        return R.OK();
    }

    @Override
    public R updateActive(GroupActive groupActive) {

        String token = request.getHeader("token");
        if (token == null) {
            return R.ERROR(500, "请先登录");
        }
        String userName = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userName", String.class);
        GroupActive groupActive1 = activeMapper.selectById(groupActive.getActiveId());
        if(groupActive1==null){
            return R.ERROR(500, "活动不存在,不允许修改");
        }
        groupActive.setUpdateBy(userName);
        groupActive.setUpdateTime(new Date());
        activeMapper.updateById(groupActive);


        return R.OK();
    }

    @Override
    public R deleteAllActive(List<GroupActive> activeList) {
        if(activeList==null||activeList.isEmpty()){
            return R.ERROR(500,"请选择活动");
        }
        for (GroupActive groupActive : activeList) {
            activeMapper.deleteById(groupActive.getActiveId());
        }
        return R.OK();
    }

    @Override
    public R deleteActive(GroupActive groupActive) {
        GroupActive groupActive1 = activeMapper.selectById(groupActive);
        if(groupActive1==null){
            return R.ERROR(500,"活动不存在");
        }
        activeMapper.deleteById(groupActive);
        return R.OK();
    }



}




